#include <jni.h>
#include <string>
#include <tgmath.h>
#include "log.h"


extern "C"
JNIEXPORT void JNICALL
Java_com_hrg_gys_clfr_myapplication_JniData_referenceDemo(JNIEnv *env, jclass clazz) {
    // TODO: implement referenceDemo()
    static jclass stringRerence = NULL;
    if (stringRerence == NULL) {
        jclass jclass1 = env->FindClass("java/lang/String");
        stringRerence = static_cast<jclass>(env->NewGlobalRef(jclass1)); /*创建Global引用*/
        env->DeleteLocalRef(jclass1); /*释放jclass1 Local引用*/
    } else {
        LOGD("used Canche");
    }
    /*使用stringRerence引用*/
    jmethodID jmethodId = env->GetMethodID(stringRerence, "<init>", "(Ljava/lang/String;)V");
    jstring jstring2 = env->NewStringUTF("java string");
    jstring jstring1 = static_cast<jstring>(env->NewObject(stringRerence, jmethodId, jstring2));

    const char *chars = env->GetStringUTFChars(jstring1, 0);
    LOGD("chars %s ", chars);

    static jclass stringRerence2 = NULL;
    if (stringRerence == NULL) {
        jclass jclass1 = env->FindClass("java/lang/String");
        stringRerence = static_cast<jclass>(env->NewWeakGlobalRef(jclass1)); /*创建Global引用*/
        env->DeleteLocalRef(jclass1); /*释放jclass1 Local引用*/
    } else {
        LOGD("used Canche");
    }
    /*使用stringRerence引用*/
//    jboolean  jboolean1=env->IsSameObject(stringRerence2, NULL);
//    jstring jstring2 = env->NewStringUTF("java string");
//    jstring jstring1 = static_cast<jstring>(env->NewObject(stringRerence, jmethodId, jstring2));
//
//    const char *chars = env->GetStringUTFChars(jstring1, 0);
    LOGD("chars %s ", chars);
}